#include "XMLPipelineElements.h"

#include <QDebug>

XMLMuxer::XMLMuxer()
        : PipelineProcessingElement(), secondaryParents()
{
}

XMLMuxer::~XMLMuxer()
{
    secondaryParents.clear();
}

void XMLMuxer::setup(const QDomNode& setupDescription)
{
}
    
void XMLMuxer::process()
{
    // process and parse the parent byte array
    QDomDocument doc;
    doc.setContent(parent->result());
    QDomElement documentElement = doc.documentElement();
    
    // process each secondary parent, fetching the events
    for (int index = 0; index < secondaryParents.size(); index++) {
        // parse the byte array
        QDomDocument secondaryDoc;
        secondaryDoc.setContent(secondaryParents.at(index)->result());
        
        // move all events from the secondary document to the main document
        QDomNode current;
        QDomNode next = secondaryDoc.documentElement().firstChild();
        while (!next.isNull()) {
            current = next;
            next = current.nextSibling();
            documentElement.appendChild(current);
        }
    }
    
    buffer = doc.toByteArray();
}
